//require jweixin-1.2.0.js
var shared = (function ($, wx,config,urlFunc) {
    //region 缓存数据
    var data = {
        title: "",
        imgUrl: "",
        link: "",
        desc: "",
        sharedId: "",
        shopId: 0,
        pageType:0,
        templink: "",
        success: function (errorMessage) {
            //region 分享成功后,回写到数据库
            var message = errorMessage.errMsg;
            var platform = 2;
            if (message.indexOf("sendAppMessage") != -1) {
                platform = 1;
            } else if (message.indexOf("shareTimeline") != -1) {
                platform = 2;
            }
            else if (message.indexOf("shareQQ") != -1) {
                platform = 3;
            }
            //todo 替换token
            var token =g.storage.getToken();
            $.ajax({
                type: "post",
                url: urlFunc.format("api/shared/create") ,
                headers: {'token':token},
                data: {
                    newId: data.sharedId,
                    oldId: data.oldId,
                    sharedShopId: data.shopId,
                    pageType:data.pageType,
                    pageTitle: data.title,
                    pageDesc: data.desc,
                    pageImg: data.pageImg,
                    pageLink: data.link
                },
                dataType: "json",
                success: function (result) {

                },
                error: function () {
                    g.box.waiting({title:"保存分享信息失败"})
                }
            });

            var newId = utils.createGUID();
            resetParams(newId);
        },
        cancel: function () {
            var newId = utils.createGUID();
            resetParams(newId);
        }
    }
    //endregion

    //region 工具类
    var utils = {
        createID: function () {
            return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
        },
        createGUID: function () {
            return this.createID() + this.createID() + this.createID() + this.createID() + this.createID() + this.createID() + this.createID() + this.createID();
        },
        processUrl: function (url, newId) {
            url = url.replace(/(sharedId=[\w\d_-]+)/, "").replace(/(sharedid=[\w\d_-]+)/, "");
            if (url.indexOf("?") != -1) {
                url = url + "&sharedId=" + newId;
            } else {
                url = url + "?sharedId=" + newId;
            }
            return url;
        },
        wxconfig: function (callback) {
            this.sign(function (timestamp, data) {
                wx.config({
                    debug: false,
                    appId: 'wx75d264c0d5ba7218',
                    timestamp: timestamp,
                    nonceStr: 'Wm3WZYTPz0wzccnW',
                    signature: data,
                    jsApiList: [
                        "onMenuShareTimeline",
                        "onMenuShareAppMessage",
                        "onMenuShareQQ",
                        "onMenuShareWeibo",
                        "onMenuShareQZone"
                    ]
                });
                wx.ready(function () {
                    callback();
                });
            })

        },
        sign: function (callback) {
            var timestamp = parseInt(new Date().getTime() / 1000);
            var token = g.storage.getToken();
            $.get(urlFunc.format("/wechat/jsconfig") , {
                url: location.href,//注意切割#后面的内容
                timestamp: timestamp
            }, function (data) {
                window.signature = data;
                callback(timestamp, data);
            })

        },
        saveShared: function () {

        }
    }
    //endregion

    //region 重置分享链接的id
    var resetParams = function (newId) {
        data.sharedId = newId;
        data.link = utils.processUrl(data.templink, newId);
        wx.onMenuShareAppMessage(data);
        wx.onMenuShareTimeline(data);
        wx.onMenuShareQQ(data);
    }
    //endregion

    //region 初始化分享
    var config = function (title, img, templink, desc,pageType, shopId) {
        var regShared=/sharedId=([\w\d_-]+)/.exec(location.href);
        var oldId = regShared?regShared[1]:"";
        data.oldId = oldId ? oldId : "";
        utils.wxconfig(function () {
            data.title = title;
            data.imgUrl = img;
            data.desc = desc;
            data.templink = templink;
            data.pageType=pageType;
            data.shopId = shopId ? shopId : 0;//shopId可有可无
            var newId = utils.createGUID();
            resetParams(newId);
        })
    }
    //endregion

    //region 对外公开接口
    return {
        config: config
    }
    //endregion

})($, wx,g.config,g.url)
//分享类型说明
//页面类型 (1:下载页面 2:店铺页面 3 知识库详情页面 4店铺列表 5:全景 6：视频)
// shared.config("哇咔咔", "https://open.weixin.qq.com/cgi-bin/openproxy?url=http%3A%2F%2Fwx.qlogo.cn%2Fmmhead%2FQ3auHgzwzM7ic5IpZKm3kX3LibBtvPUXva7pkMg2iaP6SIPxcic801sm3Q%2F0", "http://www.baidu.com?sharedId=11223sasasa_aaa", "测试描述",1, 0);